package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ddyx.entity.wxCustomer;
import com.ddyx.mapper.wxCustomerMapper;
import com.ddyx.service.IWxCustomerService;
import com.ddyx.util.DateUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class WxCustomerServiceImpl implements IWxCustomerService{

	@Resource
	wxCustomerMapper wxcustomerMapper;
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm createWxCustomer(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			
			if(wxcustomerMapper.selectWxCustomerIsExits(paramMap) > 0) {
				result.setStatus(false);
				result.setMessage("微信客户已存在");
			}
			
			paramMap.put("createTime", DateUtil.format_ymd_hms.format(new Date()));
			int index = wxcustomerMapper.createWxCustomer(paramMap);
			if(index < 0) {
				result.setStatus(false);
				result.setMessage("创建微信客户失败");
			}
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("创建微信客户失败");
			log.error(e+"创建微信客户失败");
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm selectWxCustomer(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			List<wxCustomer> customers = wxcustomerMapper.selectWxCustomer(paramMap);
			result.setData(customers);
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("查询微信客户失败");
			log.error(e+"创建微信客户失败");
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm updateWxCustomer(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			int index = wxcustomerMapper.updateWxCustomer(paramMap);
			if(index < 0) {
				result.setStatus(false);
				result.setMessage("修改微信客户失败");
			}
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("修改微信客户失败");
			log.error(e+"修改微信客户失败");
		}
		return result;
	}

}
